package com.itheima.reggie.interceptor;

import com.itheima.reggie.common.R;
import com.itheima.reggie.exception.BusinessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.FileNotFoundException;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class MyExceptionHandler implements HandlerInterceptor {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> doSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) {
        String message = e.getMessage();
        String s = message.split(" ")[2];
        return R.error(s + "用户名已经存在!");
    }

    @ExceptionHandler(FileNotFoundException.class)
    public R<String> doFileNotFoundException(FileNotFoundException e){
        return R.error("图片查找不到");
    }

    @ExceptionHandler(BusinessException.class)
    public R<String> doBusinessException(BusinessException be){
        return R.error(be.getMessage());
    }
}
